package com.yogee.topseller.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.InterestLabelBean;
import com.yogee.topseller.utils.pinyinUtilis.ContactSortModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.ItemHolder> {

    private Context context;
    private List<ContactSortModel> beans = new ArrayList<>();
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    private List<String> initSelect = new ArrayList<>();

    public CityListAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<ContactSortModel> beans,List<String> initSelect) {
        this.beans = beans;
        this.initSelect = initSelect;
        notifyDataSetChanged();
    }

    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    //获得选中条目的结果
    public ArrayList<InterestLabelBean.ListBean.LabelListBean> getSelectedItem() {
        ArrayList<InterestLabelBean.ListBean.LabelListBean> selectList = new ArrayList<>();
        for (int i = 0; i < beans.size(); i++) {
            if (isItemChecked(i)) {
                InterestLabelBean.ListBean.LabelListBean labelBean = new InterestLabelBean.ListBean.LabelListBean(beans.get(i).getId(),beans.get(i).getName());
                selectList.add(labelBean);
            }
        }
        return selectList;
    }


    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_contact, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, final int position) {
        final ContactSortModel bean = beans.get(position);
        holder.tv_city_name.setText(bean.getName());
        int section = getSectionForPosition(position);

        if (position == getPositionForSection(section)) {
            holder.tv_catagory.setVisibility(View.VISIBLE);
            holder.tv_catagory.setText(bean.getSortLetters());
        } else {
            holder.tv_catagory.setVisibility(View.GONE);
            holder.line_contact_one.setVisibility(View.GONE);
        }

        if(initSelect.contains(bean.getName())){
            setItemChecked(position,true);
        }
        else{
            setItemChecked(position,false);
        }


        if(isItemChecked(position)){
            holder.rl_select.setVisibility(View.VISIBLE);
        }else{
            holder.rl_select.setVisibility(View.GONE);
        }
        holder.ll_contact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isItemChecked(position)) {
                    setItemChecked(position, false);
                    initSelect.remove(bean.getName());
                    notifyItemChanged(position);
                }else{
                    if(getSelectedItem()!=null&&getSelectedItem().size()>=3){
                        ToastUtils.showToast(context,"选择条数不能超过3条");
                    }else{
                        setItemChecked(position, true);
                        initSelect.add(bean.getName());
                        notifyItemChanged(position);
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_catagory)
        TextView tv_catagory;
        @BindView(R.id.tv_city_name)
        TextView tv_city_name;
        @BindView(R.id.line_contact_one)
        View line_contact_one;
        @BindView(R.id.line_contact_two)
        View line_contact_two;
        @BindView(R.id.rl_select)
        RelativeLayout rl_select;
        @BindView(R.id.ll_contact)
        LinearLayout ll_contact;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public int getSectionForPosition(int position) {
        return beans.get(position).getSortLetters().charAt(0);
    }
    public int getPositionForSection(int section) {
        for (int i = 0; i < beans.size(); i++) {
            String sortStr = beans.get(i).getSortLetters();
            char firstChar = sortStr.toUpperCase().charAt(0);
            if (firstChar == section) {
                return i;
            }
        }
        return -1;
    }
}